// 权限文件
import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress' // 引入一份进度条插件
import 'nprogress/nprogress.css' // 引入进度条样式
// 白名单
const whiteList = ['/login', '/404']
// 全局路由导航守卫
router.beforeEach(async(to, from, next) => {
  // 开启进度条
  NProgress.start()
  // 判断 token 是否存在
  const token = store.getters.token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userId) {
        await store.dispatch('user/asyncSetUserInfo')
      }

      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
  // 关闭进度条
  NProgress.done()
})
